package cn.huihui.miniprogram.stuman.component.filter;

import cn.huihui.miniprogram.stuman.component.UserAuthentication;
import cn.huihui.miniprogram.stuman.database.mapper.StudentMapper;
import cn.huihui.miniprogram.stuman.database.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

@Component
public class FakeLoginFilter extends GenericFilterBean {

    @Autowired
    StudentMapper studentMapper;

    Student defaultStudent;

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
//        if (defaultStudent == null){
//        }

        defaultStudent = studentMapper.selectByPrimaryKey(1);

        //使用默认用户登录
        //{"phone":"13888888888","id":"2018034243066","sex":"男","name":"法外狂徒张三"}
        Authentication authentication = new UserAuthentication(defaultStudent);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        chain.doFilter(request, response);
    }
}
